package com.atguigu.gmall.gateway.config;

import com.atguigu.gmall.common.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

@Data
@Slf4j
@ConfigurationProperties(prefix = "jwt.auth")
public class JwtProperties implements InitializingBean {
    private String publicKeyFileName;
    private String cookieName;
    private PublicKey publicKey;
//    @PostConstruct 当前类对象创建后立即执行 可以初始化
    // InitializingBean: spring提供的 在容器对象初始化后 提供一个立即执行的方法
    @Override
    public void afterPropertiesSet() throws Exception {
        try{
            this.publicKey = RsaUtils.getPublicKey(publicKeyFileName);
        } catch (Exception e) {
            log.error("公钥初始化失败: "+ ExceptionUtils.getStackTrace(e));
        }
    }

}
